package com.kashow.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.kashow.AppContext;
import com.kashow.injector.ContextLife;
import com.kashow.injector.component.FragmentComponent;
import com.kashow.injector.module.FragmentModule;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.android.FragmentEvent;

import javax.inject.Inject;

public abstract class BaseFragment <T extends RxPresenter> extends BaseSupportFragment  implements View.OnClickListener, BaseView{
    protected FragmentComponent mFragmentComponent;
    @Inject
    protected T mPresenter;

    @Inject
    @ContextLife("Activity")
    protected Context mContext;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mFragmentComponent = DaggerFragmentComponent.builder()
                .fragmentModule(new FragmentModule(this))
                .applicationComponent(((AppContext)getActivity().getApplication()).component())
                .build();

        initInject();

        if (this instanceof BaseFragmentView) mPresenter.attachView(this);
        initEventAndData();
    }

    public  <F> LifecycleTransformer<F> bindUntilFragmentEvent(FragmentEvent event) {
        return bindUntilEvent(event);
    }

    protected abstract void initInject();

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter!= null) mPresenter.detachView();
    }
}
